import { createRouter, createWebHistory } from 'vue-router'
import VueCookie from 'vue-cookie';
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
	  {
		  path: '/',
		  name: 'Index',
		  component: () => import('@/views/sys/client'),
          children: [
            {
                path: '/',
                name: 'Indexmain',
                component: () => import('@/views/sys/client/index-part'),
				children: [
					{
						path: '/',
						name: 'Indexmainfooter',
						component: () => import('@/views/sys/client/index-part/about-us'),
					}
				]
            },
            {
                path: '/goods',
                name: 'GoodsIndex',
                props: true,
                component: () => import('@/views/sys/client/goods')
            },
			{
			    path: '/goodsInfo',
			    name: 'GoodsInfo',
			    props: true,
			    component: () => import('@/views/sys/client/goods-part')
			},
			
            {
                path: '/user',
                name: 'UserIndex',
                component: () => import('@/views/sys/client/user'),
                meta: { requiresAuth: true },// 需要登录才能访问
                children: [
                    {
                        path: "/user",
                        name: "PersonalInfo",
                        component: () => import('@/views/sys/client/user-center')
                    },
                    {
                        path: "/password-change",
                        name: "PasswordChange",
                        component: () => import('@/views/sys/client/user-center/password-change')
                    },
                    {
                        path: "/my-collection",
                        name: "MyCollection",
                        component: () => import('@/views/sys/client/user-center/my-collection')
                    },
                    {
                        path: "/my-orders",
                        name: "MyOrders",
                        component: () => import('@/views/sys/client/user-center/my-orders')
                    },
                    {
                        path: "/recharge",
                        name: "Recharge",
                        component: () => import('@/views/sys/client/user-center/recharge')
                    },
                    {
                        path: "/transfer",
                        name: "Transfer",
                        component: () => import('@/views/sys/client/user-center/transfer')
                    },
                    {
                        path: "/consumption-record",
                        name: "ConsumptionRecord",
                        component: () => import('@/views/sys/client/user-center/consumption-record')
                    }
                ]
            },
            {
                path: '/orders',
                name: 'OrdersIndex',
                component: () => import('@/views/sys/client/orders'),
                children: [
                    {
                        path: '/orders',
                        name: 'AllOrders',
                        component: () => import('@/views/sys/client/orders-part')
                    },
                    {
                        path: '/paid',
                        name: 'PaidOrders',
                        component: () => import('@/views/sys/client/orders-part/paid')
                    },
                    {
                        path: '/unpaid',
                        name: 'UnPaidOrders',
                        component: () => import('@/views/sys/client/orders-part/unpaid')
                    },
                    {
                        path: '/pickup',
                        name: 'PickUp',
                        component: () => import('@/views/sys/client/orders-part/pickup')
                    }
                ]
            },
            {
                path: '/create-orders',
                name: 'CreateOrders',
                component: () => import('@/views/sys/client/orders-part/create-orders')
            },
            {
                path: '/paid-method',
                name: 'PaidMethod',
                component: () => import('@/views/sys/client/orders-part/paid-method')
            },
            {
                path: '/pay-success',
                name: 'PaySuccess',
                component: () => import('@/views/sys/client/orders-part/pay-success')
            },
            {
                path: '/cart',
                name: 'CartIndex',
                component: () => import('@/views/sys/client/cart')
            }
        ]
	  },
	  {
		  path:'/login',
		  name: 'LoginRegister',
		  component: () => import('@/views/login')
	  },
	  {
	  		  path:'/about-us',
	  		  name: 'AboutUs',
	  		  component: () => import('@/views/sys/about-us')
	  },
	  {
	  		  path:'/contact',
	  		  name: 'Contact',
	  		  component: () => import('@/views/sys/about-us/contact')
	  },
	  {
	  		  path:'/cooperate',
	  		  name: 'Cooperate',
	  		  component: () => import('@/views/sys/about-us/cooperate')
	  },
	  {
	  		  path:'/feedback',
	  		  name: 'Feedback',
	  		  component: () => import('@/views/sys/about-us/feedback')
	  },
	  {
	  		  path:'/join',
	  		  name: 'Join',
	  		  component: () => import('@/views/sys/about-us/join')
	  }
   
  ]
})

function ToLogin(){
	router.push({ name: 'LoginRegister' });
}
router.beforeEach((to, from, next) => {
	const isLoggedIn = VueCookie.get('token'); // 从cookie中获取用户信息
	if (to.matched.some(record => record.meta.requiresAuth)) {
	  if (!isLoggedIn) {
		ElMessageBox.confirm('您尚未登录，请先登录。', '提示', {
		  confirmButtonText: '登录',
		  cancelButtonText: '取消',
		  type: 'warning',
		}).then(() => {
		  // 用户点击登录
		  ToLogin();
		  next();
		}).catch(() => {
		  // 用户点击取消
		  next(false);
		});
	  } else {
		// 用户已登录，允许访问
		next();
	  }
	} else {
	  // 路由不需要认证，直接允许访问
	  next();
	}
});


export default router
